<?php

namespace app\admin\controller\mall;

use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\model\MallManager;
use app\common\model\MallUser;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * Class Manager
 * @package app\admin\controller\mall
 * @ControllerAnnotation(title="商家中心")
 */
class Manager extends AdminController
{
    use Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model =(new MallManager());
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);

            if(!$post['openid']){
                $this->error('请选择绑定会员');
            }

            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            if($save){
                (new MallUser())->where([['openid','=',$post['openid']]])->save(['ismanager'=>1]);
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $oldopenid=$row['openid'];
            $rule = [];
            $this->validate($post, $rule);
            if(!$post['openid']){
                $this->error('请选择绑定会员');
            }
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if($post['openid'] !=$oldopenid){
                file_put_contents('log.txt',date('Y-m-d H:i:s').'更换商家会员：'.$oldopenid.'->'.$post['openid'].PHP_EOL,8);
                (new MallUser())->where([['openid','=',$post['openid']]])->save(['ismanager'=>1]);
                // 强制解绑商家
                $allmanager=$this->model->where([['openid','=',$post['openid']]])->select()->toArray();
                foreach ($allmanager as $k=>$v){
                    if($v['id'] !=$id){
                        $this->model->where([['id','=',$v['id']]])->save(['openid'=>'']);
                    }
                }
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        $userinfo =(new MallUser())->where([['openid','=',$row['openid']]])->find();
        $row['nickName'] =!empty($userinfo)?$userinfo['nickName']:'';

        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="获取用户")
     */
    public function getUserlist(){
        $keyword =$this->request->param('keyword','','trim');
        $where =[['nickName','<>','']];
        if($keyword){
            $where[] =[['nickName','like','%'.$keyword.'%']];
        }
        $list =(new MallUser())->field('openid as value,nickName as name')->where($where)->select()->toArray();
        $this->success('ok',$list);
    }

    /**
     * @NodeAnotation(title="获取未绑定用户")
     */
    public function getNobingUserlist(){
        $keyword =$this->request->param('keyword','','trim');
        $where =[['ismanager','=',0],['nickName','<>','']];
        if($keyword){
            $where[] =[['nickName','like','%'.$keyword.'%']];
        }
        $list =(new MallUser())->field('openid as value,nickName as name')->where($where)->select()->toArray();
        $this->success('ok',$list);
    }

    /**
     * @NodeAnotation(title="审核商家")
     */
    public function apply($id){
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if($save && $post['status']){
                (new MallUser())->where([['openid','=',$row['openid']]])->save(['ismanager'=>1]);
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

}